package com.yq.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yq.reggie.pojo.User;
import com.yq.reggie.service.UserService;
import com.yq.reggie.utils.SMSUtils;
import com.yq.reggie.utils.ValidateCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private StringRedisTemplate redisTemplate;
    /**
     * 发送短信验证码
     * @param user
     * @param httpSession
     */
    @PostMapping("/sendMsg")
    public void sendMsg(@RequestBody User user, HttpSession httpSession){
        //生成验证码
        String code = ValidateCodeUtils.generateValidateCode(4).toString();
        log.info("短信验证码：{}",code);
        //发送短信
/*        SMSUtils.sendMessage("","",user.getPhone(),code);*/

/*        //将短信验证码存到session中
        httpSession.setAttribute("phoneCode",code);*/
        //将短信验证码存到redis中
       redisTemplate.opsForValue().set("login_"+user.getPhone(),code,60, TimeUnit.SECONDS);
    }

    /**
     * 用户登录
     * @param map
     * @param httpSession
     * @return
     */
    @PostMapping("/login")
    public Result login(@RequestBody Map map,HttpSession httpSession){
        //获取用户填写的验证码
        Object code = map.get("code");
        String phone = (String) map.get("phone");
        //验证验证码是否正确
/*        Object phoneCode = httpSession.getAttribute("phoneCode");*/
        String phoneCode = redisTemplate.opsForValue().get("login_" + phone);
        if(phoneCode.equals(code)){
            //登录成功
                //判断是否为新用户：如果是新用户则自动注册
                LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.eq(User::getPhone,phone);
                long count = userService.count(queryWrapper);
                if(count==0){
                    //新用户自动注册
                    User user = new User();
                    user.setPhone(phone);
                    userService.save(user);
                }
                //清除redis缓存
                redisTemplate.delete("login_" + phone);
                //将用户id存入session
                User user = userService.getOne(queryWrapper);
                httpSession.setAttribute("user",user.getId());
            return Result.success("登录成功");
        }
        return Result.fail("验证码错误,请重新输入");
    }
}
